// pages/order_new/detail/index.js
const App = getApp();
Page({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    list: [{
      stepInfo: [{
          status: 0,
          pro_name: '填写订单'
        },
        {
          status: 1,
          pro_name: '在线支付'
        },
        {
          status: 2,
          pro_name: '陪诊'
        },
        {
          status: 3,
          pro_name: '服务完成'
        },
      ],
      sel_icon: "../../../images/right.png",
      pro_color: '#3DB969'
    }, ],
    order: {},
    time:300
  },
  onLoad(options) {
    let _this = this;
    _this.data.order_id = options.order_id;
    // 获取订单详情
    _this.getOrderDetail(options.order_id);
    _this.setTimeCount()
  },

  setTimeCount:function(){
    let time=this.data.time
    time--;
    if (time <= 0) {
      time = 0;
      // this.cancelOrders()
    }
    this.setData({
      time:time
    })
    setTimeout(this.setTimeCount,1000);
  },
/**
   * 格式化时间
   */
  getFormat: function (msec) {
    let ss = parseInt(msec / 1000);
    let ms = parseInt(msec % 1000);
    let mm = 0;
    let hh = 0;
    if (ss > 60) {
      mm = parseInt(ss / 60);
      ss = parseInt(ss % 60);
      if (mm > 60) {
        hh = parseInt(mm / 60);
        mm = parseInt(mm % 60);
      }
    }
    ss = ss > 9 ? ss : `0${ss}`;
    mm = mm > 9 ? mm : `0${mm}`;
    hh = hh > 9 ? hh : `0${hh}`;
    return { ss, mm, hh };
  },

  /**
   * 组件的方法列表
   */
  getOrderDetail(order_id) {
    let _this = this;
    App._get('user.order/receivedDetail', {
      order_id
    }, result => {
      _this.setData(result.data);
    });
  },
  onShow() {
    let {
      list
    } = this.data;
    list.forEach(item => {
      let onePer = item.stepInfo.filter(i0 => i0.status == 1).length;
      item.per = 100 / (item.stepInfo.length - 1) * (onePer - 1)
    })
    this.setData({
      list
    })
  },
  /**
   * 点击付款按钮
   */
  onPayOrder(e) {
    let _this = this;
    // 显示支付方式弹窗
    _this.payment();
  },

  /**
   * 选择支付方式
   */
  onSelectPayType(e) {
    let _this = this;
    // 隐藏支付方式弹窗
    _this.onTogglePayPopup();
    if (!_this.data.showPayPopup) {
      // 发起付款请求
      _this.payment(e.currentTarget.dataset.value);
    }
  },

  /**
   * 显示/隐藏支付方式弹窗
   */
  onTogglePayPopup() {
    this.setData({
      showPayPopup: !this.data.showPayPopup
    });
  },

  /**
   * 发起付款请求
   */
  payment(payType) {
    let _this = this,
      orderId = _this.data.order_id;
    // 显示loading
    wx.showLoading({
      title: '正在处理...',
    });
    App._post_form('user.order/pay', {
      order_id: orderId,
      payType:20
    }, result => {
      if (result.code === -10) {
        App.showError(result.msg);
        return false;
      }

      // 发起微信支付
      // if (result.data.pay_type == PayTypeEnum.WECHAT.value) {
        App.wxPayment({
          payment: result.data.payment,
          success() {
            wx.switchTab({
              url: '/pages/index/index',
            })
          },
          fail() {
            App.showError(result.msg.error);
          },
        });
      // }

      // 余额支付
      // if (result.data.pay_type == PayTypeEnum.BALANCE.value) {
      //   App.showSuccess(result.msg.success, () => {
      //     _this.getOrderDetail(orderId);
      //   });
      // }

    }, null, () => {
      wx.hideLoading();
    });
  },
  
 cancelOrder(e) {
   let _this = this;
   let order_id = _this.data.order_id;
   wx.showModal({
     title: "提示",
     content: "确认取消订单？",
     success(o) {
       if (o.confirm) {
         App._post_form('user.order/cancel', {
           order_id
         }, result => {
           wx.navigateBack();
         });
       }
     }
   });
 },
 cancelOrders(e) {
  let _this = this;
  let order_id = _this.data.order_id;
 
        App._post_form('user.order/cancel', {
          order_id
        }, result => {
          wx.navigateBack();
        })
},
callPhone(e){
  wx.makePhoneCall({
    phoneNumber: e.currentTarget.dataset.phone,
  })
}
})